package com.chainyoung.trademarket.adapter;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.common.view.CircleImageView;
import com.chainyoung.component_tmperson.ui.activity.dapp.BrowserActivity;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.entity.IndexAppItem;

import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class IndexAppListAdapter extends BaseQuickAdapter<IndexAppItem, BaseViewHolder> {

    private ImageLoader mImageLoader;
    private final static String WEB = "web";
    private final static String APP = "app";
    private final static String TEXT = "text";
    private final static String APP_ID = "appid";


    public IndexAppListAdapter(@Nullable List<IndexAppItem> data) {
        super(R.layout.layout_index_app_item, data);
        mImageLoader = Utils.getAppComponent().imageLoader();
    }


    @Override
    protected void convert(BaseViewHolder helper, final IndexAppItem item) {
        helper.setText(R.id.tv_title, item.getTitle());
        helper.setText(R.id.tv_content, item.getIntro());
        mImageLoader.loadImage(mContext,
                GlideImageConfig
                        .builder()
                        .url(item.getIcon())
                        .errorPic(R.mipmap.ic_default)
                        .transformation(new RoundedCorners(360))
                        .imagerView((ImageView) (helper.getView(R.id.iv_logo)))
                        .build());
        helper.getView(R.id.rl_app).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (item.getType()) {
                    case WEB:
                        String url=TextUtils.isEmpty(item.getUrl())?LibConstant.BROWSER_INDEX_URL:item.getUrl();
                        launch(url,url);
                        break;
                    case APP:
                        if (!TextUtils.isEmpty(item.getPackage_name())) {
                            if (!startApp(item.getPackage_name())) {
                                launch(item.getUrl(), item.getUrl());
                            }
                        }
                        break;
                    case TEXT:
                        break;
                    case APP_ID:
                        switch (item.getAppid()) {
                            case "1":
                                //行情界面
                                RouterUtils.getInstance()
                                        .build(RouterConstants.PATH_MARKET_MAIN)
                                        .navigation(mContext);
                                break;
                            case "2":
                                //Dapp 浏览器界面
                                RouterUtils.getInstance()
                                        .build(RouterConstants.PATH_OTHERAPP)
                                        .withString("title", LibConstant.BROWSER_INDEX_URL)
                                        .withString(URL, LibConstant.BROWSER_INDEX_URL)
                                        .navigation(mContext);
                                break;
                            case "3":
                                //借贷界面
                                RouterUtils.getInstance()
                                        .build(RouterConstants.PATH_DEBIT)
                                        .navigation(mContext);
                                break;
                            case "4":
                                //增益界面
                                RouterUtils.getInstance()
                                        .build(RouterConstants.PATH_FINANCING)
                                        .navigation(mContext);
                                break;
                            case "5":
                                //资讯界面
                                RouterUtils.getInstance()
                                        .build(RouterConstants.PATH_INFORMATION)
                                        .navigation(mContext);
                                break;
                        }
                        break;
                }
            }
        });

    }

    private boolean startApp(String pkgName) {
        PackageManager packageManager = mContext.getPackageManager();
        Intent intent = new Intent();
        intent = packageManager.getLaunchIntentForPackage(pkgName);
        if (intent == null) {
            return false;
        } else {
            mContext.startActivity(intent);
            return true;
        }
    }

    private void launch(String url, String name) {
        RouterUtils.getInstance()
                .build(RouterConstants.PATH_OTHERAPP)
                .withString("title", url)
                .withString(URL, url)
                .navigation(mContext);
    }


}
